package com.supor.aiot.mine.ui.setting;


import android.content.Intent;
import android.view.View;

import com.android.baseconfig.common.utils.SystemUtils;
import com.android.baseconfig.common.utils.ToastUtils;
import com.supor.aiot.common.app.SuporApplication;
import com.supor.aiot.common.base.BaseMVVMActivity;
import com.supor.aiot.common.entity.Result;
import com.supor.aiot.common.utils.MarketUtils;
import com.supor.aiot.mine.R;
import com.supor.aiot.mine.databinding.ActivityVersionUpdateBinding;

public class VersionUpdateActivity extends BaseMVVMActivity<VersionUpdateViewModel, ActivityVersionUpdateBinding> implements View.OnClickListener {

    @Override
    protected int resourceId() {
        return R.layout.activity_version_update;
    }

    @Override
    protected void initView() {
        super.initView();
        bindingView.setViewM(this);
        bindingView.tvVersion.setText(getString(R.string.version_prefix) + SystemUtils.getVerName(SuporApplication.getInstance()));
        bindingView.toolbar.setIvLeftOnClickListener(v -> finish());
        bindingView.toolbar.setTvLeftOnClickListener(v -> finish());
        bindingView.rlFunctionIntroduction.setOnClickListener(this);
        bindingView.rlVersionUpdate.setOnClickListener(this);
    }

    @Override
    protected void initData() {
        super.initData();
        viewModel.getLatestResult().observe(this, result -> {
            if (result instanceof Result.Success) {
                boolean isLatestVer = (boolean) result.getData();
                if (!isLatestVer) {
                    MarketUtils.getTools().startMarket(this, "com.supor.aiot");
                    return;
                }
                ToastUtils.getInstance().showCustomToast(getString(R.string.no_version_update));
            } else if (result instanceof Result.Error) {
                ToastUtils.getInstance().showCustomToast("获取版本更新信息失败: " + ((Result.Error) result).getErrorMsg());
            }
        });
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.rl_function_introduction) {
            startActivity(new Intent(this, FunctionIntroductionActivity.class));
        } else if (id == R.id.rl_version_update) {
            viewModel.getLatestVersion();
        }
    }
}